package com.player.thirdGroup.controller;

import com.player.thirdGroup.entity.User;
import com.player.thirdGroup.service.UserService;
import com.player.thirdGroup.util.BootStrapPage;
import com.player.thirdGroup.util.BootStrapTablePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/boot")
public class TestController {

    @Autowired
    private UserService userService;

    @RequestMapping("/page")
    public String page(){
        return "boot-page";
    }

//    @RequestMapping("/getData")
//    @ResponseBody
//    public BootStrapPage<User> getData(BootStrapTablePage page){
//        //分页信息
//        Pageable pageable = new PageRequest(page.getNowPage(),page.getLimit());
//        Specification<User> query = new Specification<User>() {
//            @Override
//            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                Map<String, String> searchMap = page.getSearchMap();//查询条件
////                判断是否含有查询条件入没有进行返回
//                Set<String> searchMapKeySet = searchMap.keySet();
//                if (searchMapKeySet.size() == 0){
//                    return null;
//                }
////                包含查询条件
//                List<Predicate> predicates = new ArrayList<>();
//                for (String searchMapKey : searchMapKeySet){
//                    predicates.add(cb.equal(root.get(searchMapKey),searchMap.get(searchMapKey)));
//                }
//                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
//            }
//        };
//        Page<User> sysUserPage = userService.findAll(query, pageable);
//        BootStrapPage<User> sysUserBootStrapPage = new BootStrapPage<User>(sysUserPage.getContent(),sysUserPage.getTotalElements());
//        return sysUserBootStrapPage;
//    }
/*
    public Page<SysUser> getStudents(BootStrapTablePage page) {
//        排序
        Sort sort = new Sort("id");
//      或 Sort sort = new Sort(Direction.ASC,"id");

    }*/
}
